package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Api(tags = "01.资讯管理模块")
@Slf4j
//@Controller
@RestController//组合注解
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
//    @ResponseBody
    @ApiOperation("添加资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult add(NoticeAddParam noticeAddParam) {
        //System.out.println("noticeAddParam = " + noticeAddParam);
        log.debug("noticeAddParam 打印一下看看数据=" + noticeAddParam);
        Notice notice = new Notice();
//        notice.setTitle(noticeAddParam.getTitle());
//        notice.setContent(noticeAddParam.getContent());
//        notice.setType(noticeAddParam.getType());
//        notice.setStatus(noticeAddParam.getStatus());
        //复制属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        //处理其他属性
        notice.setUserId(100L);//暂时写死
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
       // return new JsonResult(1000,"添加资讯成功",null);
       // return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }

    /**
     * 查询详情列表
     * @param noticeAddQuery
     * @return
     */
    @GetMapping("list")
  //  @ResponseBody
    @ApiOperation("查询详情列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeAddQuery noticeAddQuery) {
        //获取客户端传过来的属性值
        log.debug("noticeAddQuery = " + noticeAddQuery);
        String title =noticeAddQuery.getTitle();
        Integer type =noticeAddQuery.getType();
        //根据值需要到库里执行查询操作，并返回一个集合
        //设计到mybatis和springboot
        List<NoticeListVO> noticeListVOS = noticeMapper.list(title, type);
       //return new JsonResult(1000,"查询资讯列表成功",noticeListVOS);
       // return new JsonResult(StatusCode.OPERATION_SUCCESS,noticeListVOS);
        return JsonResult.ok(noticeListVOS);

    }

    @GetMapping("detail")
   // @ResponseBody
    @ApiOperation("查询详情")
    @ApiOperationSupport(order = 30)
    public JsonResult detail(Long id) {
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        //return noticeDetailInfoVO;
        //return new JsonResult(StatusCode.OPERATION_SUCCESS,noticeDetailInfoVO);
        return JsonResult.ok(noticeDetailInfoVO);
    }

    @PostMapping("delete")
   // @ResponseBody
    @ApiOperation("删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult delete(Long id) {
       log.debug("id = " + id);
        int rows =noticeMapper.delete(id);
        if (rows == 0) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }else {
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
            return JsonResult.ok();
        }
    }

    @PostMapping("update")
    //@ResponseBody
    @ApiOperation("修改资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult update(NoticeUpdateParam noticeUpdateParam) {
       log.debug("noticeUpdateParam = " + noticeUpdateParam);
        //更新数据使用实体类
        Notice notice = new Notice();
        //复制数据
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        //处理必须要处理的数据：更新的时间
        notice.setUpdateTime(new Date());
        //调用接口的方法
        int rows = noticeMapper.update(notice);
        if (rows == 0) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        return JsonResult.ok();
    }
}
